package com.cdelink.xzgy.ftp.user.service.impl;

import com.cdelink.xzgy.ftp.user.entity.Records;
import com.cdelink.xzgy.ftp.user.entity.User;
import com.cdelink.xzgy.ftp.user.mapper.one.RecordsMapper;
import com.cdelink.xzgy.ftp.user.mapper.one.UserMapper;
import com.cdelink.xzgy.ftp.user.mapper.two.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

import static com.cdelink.xzgy.ftp.user.service.impl.UserServiceImpl.EXECUTE_XZGY_FTP;
import static com.cdelink.xzgy.ftp.user.service.impl.UserServiceImpl.EXECUTE_XZGY_MIS;

/**
 * @author shixuelian
 */
@Service
public class RecordServiceImpl {
    @Resource
    private RecordsMapper recordsMapper;

    private static RecordServiceImpl recordService;
    @Autowired
    UserServiceImpl userServiceImpl;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;


    @PostConstruct
    public void init() {
        recordService = this;
        recordService.recordsMapper = this.recordsMapper;
        recordService.userMapper = this.userMapper;
        recordService.userInfoMapper = this.userInfoMapper;
    }

    /**
     * 插入操作记录的静态方法
     *
     * @param userId  用户id
     * @param type    新增/删除/替换
     * @param details 操作详情(用户+文件名)
     * @return
     */
    public static boolean insert(String userId, String type, String details, boolean directory, boolean success) {
        if (!success) {
            return false;
        }
        try {
            Records records = new Records();
            String fileName = null;

            if (!directory) {
                //文件
                int lastIndex = details.lastIndexOf(".");
                int index = details.lastIndexOf("/");
                if (index == 0) {
                    //根目录
                    records.setFolder("/");
                } else {
                    records.setFolder(details.substring(0, index));
                }
                records.setSuffixName(details.substring(lastIndex + 1));
                records.setFileName(details);
                fileName = details.substring(index + 1);
            } else {
                //文件夹
                int index = details.lastIndexOf("/");
                if (index == 0) {
                    //根目录
                    records.setFolder("/");
                } else {
                    records.setFolder(details.substring(0, index));
                }
                records.setFileName(details);
                fileName = details.substring(index + 1);
            }

            //操作者ID
            records.setOperateId(userId);
            //操作类型
            records.setType(type);
            int config = recordService.userServiceImpl.config;
            User user = null;
            if (config == EXECUTE_XZGY_MIS) {
                user = recordService.userInfoMapper.queryById(userId);
            } else if (config == EXECUTE_XZGY_FTP) {
                user = recordService.userInfoMapper.queryById(userId);
            } else {
                System.err.println("数据库中未指定数据源配置");
            }
            String name = user != null ? user.getName() : "";
            records.setName(name + "(" + user.getAccount() + ")");
            records.setDetail(name + type + (directory ? "文件夹" : "文件") + " " + fileName);
            records.setCreateTime(new Date());
            int insert = recordService.recordsMapper.insert(records);
            return insert > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("记录文件操作失败");
        }
        return false;
    }


}
